package ${project.group}.${project.name}.vehicle;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.blankj.utilcode.util.ToastUtils;
import com.chenenyu.router.Router;
import com.chenenyu.router.annotation.InjectParam;
import com.chenenyu.router.annotation.Route;
import com.idogfooding.backbone.network.ApiException;
import com.idogfooding.backbone.ui.AndroidBug5497Workaround;
import ${project.group}.${project.name}.Const;
import ${project.group}.${project.name}.R;
import ${project.group}.${project.name}.base.AppBaseActivity;
import ${project.group}.${project.name}.network.Api;
import ${project.group}.${project.name}.network.ApiCallback;
import ${project.group}.${project.name}.network.ApiHttpResult;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;



import butterknife.BindView;
import butterknife.OnClick;

import static ${project.group}.${project.name}.network.Api.RESPONSE_EXPIRED;


/**
 * 添加车辆
 *
 * @author Charles
 */
@Route(value = "VehicleView", interceptors = "Login")
public class VehicleViewActivity extends AppBaseActivity {

    @InjectParam
    Vehicle data;
    @InjectParam
    String viewType = Const.ViewType.VIEW;

    @BindView(R.id.et_item0)
    TextView etItem0;
    @BindView(R.id.et_item1)
    TextView etItem1;
    @BindView(R.id.et_item2)
    TextView etItem2;
    @BindView(R.id.et_item3)
    TextView etItem3;

    @Override
    protected int getLayoutId() {
        return R.layout.vehicle_view;
    }

    @Override
    protected void onSetup(Bundle savedInstanceState) {
        this.showToolbar = true;
        super.onSetup(savedInstanceState);
        AndroidBug5497Workaround.assistActivity(this);
        Router.injectParams(this);
        if (data!=null) {
            toolbar.setTitleMainText("详情");
            fillViews();
        } else {
            ToastUtils.showLong("页面数据传参错误");
            finish();
        }
    }

    private void fillViews() {

        if (Const.ViewType.VIEW.equalsIgnoreCase(viewType)) {
            setTitle(data.getVehicleNo());
            etItem0.setText(data.getVehicleNo());
            etItem1.setTag(data.getCatCd());
            etItem2.setText(data.getDriverName());
            etItem3.setText(data.getMobile());
        }
    }
    private void delete(String id) {
        OkGo.<ApiHttpResult<Vehicle>>delete(Api.VEC_DELETE + id)
                .tag(this)
                .execute(new ApiCallback<ApiHttpResult<Vehicle>>(this, true) {
                    @Override
                    public void onSuccessData(Response<ApiHttpResult<Vehicle>> response, ApiHttpResult<Vehicle> result) {
                        if (result.getCode() == 0) {
                            if (result.getData() != null) {
                                data = result.getData();
                                fillViews();
                            }
                        }
                    }

                    @Override
                    protected void onApiError
                            (Response<ApiHttpResult<Vehicle>> response,
                             ApiException exception) {
                        if (exception.getCode() == RESPONSE_EXPIRED) {
                            super.onApiError(response, exception);
                        }
                    }
                });
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent intent) {
        if (null != intent && resultCode == RESULT_OK && (requestCode == Const.RequestCode.PHOTOS2_PREVIEW)) {

        } else {
            super.onActivityResult(requestCode, resultCode, intent);
        }
    }

    @OnClick({R.id.btn_left,R.id.btn_right})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_left:
                Router.build("VehicleAdd")
                        .with("data",data)
                        .with("viewType", Const.ViewType.EDIT)
                        .go(this);
                break;
            case R.id.btn_right:
               //删除
                delete(data.getId());
                break;
        }
    }

}
